package com.dingreading.cloud.soms.service.impl;

import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.soms.dto.StoreProductLogDto;
import com.dingreading.cloud.soms.entity.StoreProductLogs;
import com.dingreading.cloud.soms.entity.table.StoreProductLogsTableDef;
import com.dingreading.cloud.soms.mapper.StoreProductLogsMapper;
import com.dingreading.cloud.soms.service.StoreProductLogsService;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2024-09-11
 */
@Service
public class StoreProductLogsServiceImpl extends BaseServiceImpl<StoreProductLogsMapper, StoreProductLogs, StoreProductLogsTableDef> implements StoreProductLogsService {

    @Override
    protected StoreProductLogsTableDef getTable() {
        return StoreProductLogsTableDef.storeProductLogs;
    }

    @Override
    public int dropByProductUid(String storeUid, String uid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.productUid.eq(uid));
        if (StringUtils.isNotBlank(storeUid))
            condition.and(table.storeUid.eq(storeUid))
                    .and(table.source.eq(2));
        else
            condition.and(table.source.eq(1));
        return getMapper().deleteByCondition(condition);
    }

    @Override
    public List<StoreProductLogs> listByStoreUidAndProductUid(String storeUid, String uid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.productUid.eq(uid));
        if (StringUtils.isNotBlank(storeUid))
            condition.and(table.storeUid.eq(storeUid));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.asc());

        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public List<StoreProductLogDto> dtoListByStoreUidAndProductUid(String storeUid, String uid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.productUid.eq(uid));
        if (StringUtils.isNotBlank(storeUid))
            condition.and(table.storeUid.eq(storeUid));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.asc());

        return getMapper().selectListByQueryAs(wrapper, StoreProductLogDto.class);
    }

    @Override
    public List<StoreProductLogs> listByProductUids(List<String> productUids) {
        return getMapper().selectListByCondition(table.productUid.in(productUids));
    }

    @Override
    public List<StoreProductLogDto> dtoListByProductUidIn(List<String> productUids) {
        QueryWrapper wrapper = QueryWrapper.create()
                .where(table.productUid.in(productUids))
                .orderBy(table.id.asc());

        return getMapper().selectListByQueryAs(wrapper, StoreProductLogDto.class);
    }

    @Override
    public List<StoreProductLogs> listByUids(List<String> uids) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.uid.in(uids));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.asc());

        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public boolean haveProjectUid(String storeUid, String projectUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(storeUid))
            condition.and(table.storeUid.eq(storeUid));
        condition.and(table.projectUid.eq(projectUid));

        return exists(condition);
    }

    @Override
    public void updateEnabledByProductUid(String storeUid, String productUid, Integer enabled, String staffUid, String staffName) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.productUid.eq(productUid));
        if (StringUtils.isNotBlank(storeUid))
            condition.and(table.storeUid.eq(storeUid))
                    .and(table.source.eq(2));
        else
            condition.and(table.source.eq(1));

        UpdateChain.of(StoreProductLogs.class)
                .set(table.enabled, enabled)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(condition)
                .update();
    }

}
